<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>${siteName}</title>
        <%@ include file="/common/commonstyle.jsp" %>
        <link href="${base}/js/form/themes/Default/style/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="${base}/js/form/formValidator-4.1.3.min.js"></script>
        <script type="text/javascript" src="${base}/js/form/formValidatorRegex.js"></script>
        <script type="text/javascript" src="${base}/js/jquery.placeholder.js"></script>
        <script type="text/javascript" src="${base}/js/angular/lib/angular.min.js"></script>
        <script type="text/javascript" src="${base}/js/angular/lib/angular-resource.min.js"></script>
        <script type="text/javascript">
        'use strict';
        var global = {};
        angular.module("app", ['ngResource']).
        config(function($httpProvider) {
            $httpProvider.defaults.headers["delete"] = {
                'Content-Type': 'application/json;charset=utf-8'
            };
        }).
        factory("$User", ["$resource", function($resource) {
            return $resource('${base}/frontUserCtl/api/user');
        }]).
        factory("$Address", ["$resource", function($resource) {
            return $resource('${base}/siteCtl/api/address',{},{
                remove: {
                    method:"PUT"
                }
            });
        }]).
        factory("$Coupon", ["$resource", function($resource) {
            return $resource('${base}/api/coupon',{},{
                remove: {
                    method:"PUT"
                }
            });
        }]).
        factory("$Order", ["$resource", function($resource) {
            return $resource('${base}/orderFrontCtl/api/order/:id');
        }]).
        filter("sexLabel", function() {
            return function(v) {
                return v == 1 ? '男' : '女';
            }
        }).
        controller("UserController", ["$scope", "$User", "$Address", function($scope, $User, $Address){
            $scope.user = $User.get();
            $scope.addresses = $Address.query();
            $scope.address = {};
            $scope.edit = false;
            $scope.editAddress = false;
            $scope.toEditUser = function() {
                $scope.edit = true;
                $scope._user = global.user = {
                    username: $scope.user.username,
                    email: $scope.user.email,
                    password: '',
                    birthday: $scope.user.birthday,
                    sex: $scope.user.sex,
                    address: $scope.user.address,
                    oldpassword: ''
                };
            };
            $scope.saveUser = function() {
                $scope._user.userid = $scope.user.userid;
                $User.save($scope._user, function() {
                    alert('成功');
                    $scope.user = $User.get();
                    $scope.edit=false;
                }, function(exception) {
                    alert(exception.data);
                });
            };
            $scope.toEditAddress = function(entity) {
                $scope.editAddress = true;
                $scope.address = {
                    siteid: entity.siteid,
                    siteinfo: entity.siteinfo,
                    username: entity.username,
                    phone: entity.phone
                }
            };
            $scope.toAddAddress = function() {
                $scope.editAddress = true;
                $scope.address = {
                    siteinfo: '',
                    username: '',
                    phone: ''
                }
            };
            $scope.saveAddress = function() {
                $Address.save($scope.address, function() {
                    alert('更新成功');
                    $scope.addresses = $Address.query();
                    $scope.editAddress = false;
                }, function(exception) {
                    alert(exception.data);
                });
            };
            $scope.removeAddress = function(entity) {
                if(!confirm("你确定要删除该条配送信息吗?"))
                    return;
                $Address.remove({siteid:entity.siteid}, function() {
                    alert('删除成功');
                    $scope.addresses = $Address.query();
                }, function(exception) {
                    alert(exception.data);
                });
            }
        }]).
        controller("OrderController", ["$scope", "$Order", function($scope, $Order) {
            $scope.pageNumber = 1;
            $Order.get({pageNumber: $scope.pageNumber}, function(results) {
                $scope.orders = results.resultList;
            });
            $scope.show = false;
            $scope.toViewOrder = function(orderid) {
                $scope.show = true;
                $scope.items = $Order.query({id: orderid});
            }
        }]).
        controller("CouponController", ["$scope", "$Coupon", function($scope, $Coupon) {
            $scope.coupons = $Coupon.query();
            $scope.removeCoupon = function(entity) {
                if(!confirm("你确定要删除该条优惠券吗?"))
                    return;
                $Coupon.remove(entity, function() {
                    alert('删除成功');
                    $scope.coupons = $Coupon.query();
                }, function(exception) {
                    alert(exception.data);
                });
            };
            $scope.saveCoupon = function(code) {
                if(!confirm("你确定要添加该优惠券（" + code + "）吗?"))
                    return;
                $Coupon.save({code: code}, function() {
                    alert('更新成功');
                    $scope.coupons = $Coupon.query();
                }, function(exception) {
                    alert(exception.data);
                });
            };
        }]).
        run(["$rootScope", function($rootScope) {
            global.rootScope = $rootScope;
        }]);
        
        $(function() {
            angular.bootstrap(document, ['app']);
            $('input, textarea').placeholder();
        });
        </script>
        <style type="text/css">
            label {
                cursor: pointer;
                display: inline-block;
                margin: 0 0.25em 0 0;
            }
            input {
                height: 20px;
            }
            input, select, textarea {
                    background-color: #fcfcfc;
                    border: 1px solid #999;
                    font-size: 1em;
                    padding: 0.2em 0.4em;
            }
            td {
                height: 30px;
                text-align: center;
            }
            th {
                text-align: center;
            }
            .fragment {
                width: 910px;
                height: 100%;
                line-height: 25px;
                margin-bottom: 25px;
            }
            .fragment .label {
                height: 32px;
                width: 100%;
                background-color: #E7E7E5;
                padding-left: 20px;
                padding-top: 5px;
                font-weight: bold;
                font-size: 16px;
            }
            .fragment .content {
                width: 100%;
                padding-top: 20px;
                padding-left:35px;
            }
            .opt {
                color: #26B359;
            }
        </style>
        <script type="text/javascript">
            $(function() {
                $.formValidator.initConfig({ formID: "form1", onError: function () { alert("校验没有通过，具体错误请看错误提示") } });
                $("#email").formValidator({ onShow: "请输入你的email", onfocus: "请注意你输入的email格式，例如:wzmaodong@126.com", onCorrect: "谢谢你的合作，你的email正确" }).regexValidator({ regExp: "email", dataType: "enum", onError: "email格式不正确" });
		$("#repassword").formValidator({onShow:"输再次输入密码",onFocus:"至少6个长度",onCorrect:"密码一致"}).inputValidator({min:6, max:25, empty:{leftEmpty:false,rightEmpty:false,emptyError:"重复密码两边不能有空符号"},onError:"重复密码为6-25个字符,请确认"}).compareValidator({desID:"password",operateor:"=",onError:"两次密码不一致,请确认"});
		$("#addressTo").formValidator({onShow:"请输入送餐地址"}).inputValidator({min:1, onError:"请输入送餐地址"});
                $("#contact").formValidator({onShow:"请输入联系人"}).inputValidator({min:1, onError:"请输入联系人"});
                $("#phone").formValidator({onShow:"请输入联系电话"}).inputValidator({min:1, onError:"请输入联系电话"});
            });
        </script>
    </head>
	
    <body>
        <div class="main">
            <%@ include file="/common/header.jsp" %>
            <div id="content">
                <!-- 个人信息开始 -->
                <div class="fragment" ng-controller="UserController">
                    <div class="label">个人信息</div>
                    <div class="content">
                        <ol class="property-list" ng-show="!edit">
                            <li class="fieldcontain">
                                <span class="property-label">姓名：</span>
                                <span class="property-value">{{user.username}}</span>
                            </li>
                            <li class="fieldcontain">
                                <span class="property-label">电邮：</span>
                                <span class="property-value">{{user.email}}</span>
                            </li>
                            <li class="fieldcontain">
                                <span class="property-label">生日：</span>
                                <span class="property-value">{{user.birthday}}</span>
                            </li>
                            <li class="fieldcontain">
                                <span class="property-label">性别：</span>
                                <span class="property-value">{{user.sex | sexLabel}}</span>
                            </li>
                            <li class="fieldcontain">
                                <span class="property-label">地址：</span>
                                <span class="property-value">{{user.address}}</span>
                                <span style="float:right;padding-right: 50px"><a class="opt" href="javascript:;" ng-click="toEditUser()">[编辑]</a></span>
                            </li>
                        </ol>
                        
                        <table cellpadding="0" cellspacing="0" border="0" ng-show="edit">
                            <tr>
                                <td align="right">姓名：</td>
                                <td><input ng-model="_user.username" /></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">电邮：</td>
                                <td><input id="email" ng-model="_user.email" placeholder="我们会向此邮箱发送订单确认信息"/></td>
                                <td><div id="emailTip"></div></td>
                            </tr>
                            <tr>
                                <td align="right">新密码：</td>
                                <td><input id="password" type="password" ng-model="_user.password" /></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">确认密码：</td>
                                <td><input id="repassword" type="password"/></td>
                                <td><div id="repasswordTip"></div></td>
                            </tr>
                            <tr>
                                <td align="right">生日：</td>
                                <td><input ng-model="_user.birthday" class="Wdate" onchange="global.user.birthday = this.value" onfocus="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd'})"/></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">性别：</td>
                                <td>
                                    <label><input type="radio" ng-model="_user.sex" value="1" />&nbsp;男</label>&nbsp;&nbsp;
                                    <label><input type="radio" ng-model="_user.sex" value="0" />&nbsp;女</label>
                                </td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">会员通讯：</td>
                                <td><input ng-model="_user.address" placeholder="电邮、微信、短信"/></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">旧密码：</td>
                                <td><input ng-model="_user.oldpassword" placeholder="修改以上信息需要提供旧密码"/></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">&nbsp;</td>
                                <td>
                                    <button class="btn" ng-click="saveUser();">保存</button>
                                    <button class="btn" ng-click="edit=false">取消</button>
                                </td>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                        <br/>
                        <div><b>地址管理</b><div style="float:right;padding-right: 50px"><a class="opt" href="javascript:;" ng-click="toAddAddress();">[添加]</a></div></div>
                        <ol class="property-list" ng-repeat="row in addresses">
                            <li class="fieldcontain">
                                <span class="property-label">送餐地址：</span>
                                <span class="property-value">{{row.siteinfo}}</span>
                            </li>
                            <li class="fieldcontain">
                                <span class="property-label">联系人：</span>
                                <span class="property-value">{{row.username}}</span>
                            </li>
                            <li class="fieldcontain">
                                <span class="property-label">联系电话：</span>
                                <span class="property-value">{{row.phone}}</span>
                                <span style="float:right;padding-right: 50px">
                                    <a href="javascript:;" class="opt" ng-click="toEditAddress(row);">[修改]</a>
                                    <a href="javascript:;" class="opt" ng-click="removeAddress(row);">[删除]</a>
                                </span>
                            </li>
                            <li>
                                
                            </li>
                        </ol>
                        
                        <table cellpadding="0" cellspacing="0" border="0" ng-show="editAddress">
                            <tr>
                                <td align="right">送餐地址：</td>
                                <td><input id="addressTo" ng-model="address.siteinfo"/></td>
                                <td>
                                    <div id="addressToTip"></div>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">联系人：</td>
                                <td><input id="contact" ng-model="address.username"/></td>
                                <td>
                                    <div id="contactTip"></div>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">联系电话：</td>
                                <td><input id="phone" ng-model="address.phone"/></td>
                                <td>
                                    <div id="phoneTip"></div>
                                </td>
                            </tr>
                            <tr>
                                <td></td>
                                <td>
                                    <button class="btn" ng-click="saveAddress();" ng-disabled="!(address.siteinfo&&address.username&&address.phone)">保存</button>
                                    <button class="btn" ng-click="editAddress=false">取消</button>
                                </td>
                                <td></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <!-- 个人信息结束 -->
                <!-- 近期订单开始 -->
                <div class="fragment" ng-controller="OrderController">
                    <div class="label">近期订单</div>
                    <div class="content">
                        <table cellpadding="0" cellspacing="0" border="1" width="95%">
                            <thead>
                                <tr>
                                    <th align="center" width="150">订单号</th>
                                    <th align="center" width="100">下单时间</th>
                                    <th align="center" width="80">订单状态</th>
                                    <th align="center">具体信息</th>
                                    <th align="center" width="80">金额（元）</th>
                                    <th align="center" width="80">修改送餐日期</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="row in orders">
                                    <td><b><a href="javascript:;" ng-click="toViewOrder(row.orderid)">{{row.orderid}}</a></b></td>
                                    <td>{{row.createdate}}</td>
                                    <td>{{row.status}}</td>
                                    <td>&nbsp;</td>
                                    <td>{{row.totalprice}}</td>
                                    <td>[修改]</td>
                                </tr>
                            </tbody>
                        </table>
                        <br/>
                        <div ng-show="show">
                            <div style="font-size:14px;font-weight: bold">订单具体信息</div>
                            <table cellpadding="5" cellspacing="5" border="0" width="95%">
                                <thead>
                                    <tr style="background-color:#F1F1EF">
                                        <th align="center" width="150">餐品编号</th>
                                        <th align="center" width="150">餐品名称</th>
                                        <th align="center" width="50">数量</th>
                                        <th align="center" width="50">单价</th>
                                        <th align="center" width="50">合计</th>
                                        <th>备注</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr ng-repeat="row in items">
                                        <td>{{row.orderdishid}}</td>
                                        <td>{{row.datatypeStr}}</td>
                                        <td>{{row.datacount}}</td>
                                        <td>{{row.datacount}}</td>
                                        <td>{{row.datacount}}</td>
                                        <td>{{row.datacount}}</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- 近期订单结束 -->
                <!-- 优惠码开始 -->
                <div class="fragment" ng-controller="CouponController">
                    <div class="label">优惠码</div>
                    <div class="content">
                        <ol>
                            <li ng-repeat="row in coupons">{{row.code}}&nbsp;
                                <span style="float:right;padding-right: 50px">
                                    <a class="opt"  href="javascript:;" ng-click="removeCoupon(row)">[删除]</a>
                                </span>
                            </li>
                        </ol>
                        <br/>
                        <fieldset class="form">
                            <div class="fieldcontain">
                                <label for="realname">输入优惠码：</label>
                                <input ng-model="coupon"/>&nbsp;&nbsp;
                                <button class="btn" ng-click="saveCoupon(coupon)" ng-disabled="!coupon">添加</button>
                            </div>
                        </fieldset>
                    </div>
                </div>
                <!-- 优惠码结束 -->
            </div>
            <div class="clear"></div>
            <%@ include file="/common/footer.jsp" %>
        </div>
    </body>
        
</html>